package com.berwin.bmfb.entity;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * BMFont Builder
 *
 * @author tangbowen
 * @descript 用于生成和编辑BMFont字体，*.fnt & *.png
 * 注意BMFont里面一些字段意思
 * <p>
 * ①.info
 * face - png图片的名字
 * size - 字体高度(使用某个字符图片最大高度)
 * padding - PNG图集图片间隔(0,0,0,0)
 * spacing - 空格(0,0)
 * <p>
 * bold - 加粗(默认:0)
 * italic - 斜体(默认:0)
 * charset - 字符(默认:"")
 * unicode - 编码(默认:0)
 * stretchH - 100
 * smooth - 1
 * aa - 1
 * <p>
 * ②.common
 * lineHeight - 字体高度
 * base - 基准线(size的一半)
 * scaleW - PNG宽度
 * scaleH - PNG高度
 * pages - 1
 * packed - 0
 * alphaChnl - 1
 * redChnl - 0
 * greenChnl - 0
 * blueChnl - 0
 * <p>
 * ③.editor
 * id - 编辑器名字(默认:"BMFont Builder")
 * version - 编辑器版本(默认:"1.0.0")
 * padding - 0
 * extSpacing - 0
 * <p>
 * ④.page
 * id - 0
 * file - png图片路径(默认:"*.png")
 * <p>
 * ⑤.字符信息
 * chars count = ?
 * char
 * ....
 */
public class BMFont {

    // -------------------------------------- 🔽 info 🔽 --------------------------------------
    // 字体集图片名字
    private String face = ""; // 字体名字 UI
    private int size = 0; // 字体高度 同 charHeight 属性 UI
    private int padding = 0; // Padding(图集之间的间隙) *.fnt 为:0,0,0,0 UI
    private int spacing = 0;

    // 使用默认值，一般不修改
    private int bold = 0; // 保持默认
    private int italic = 0; // 保持默认
    private String charset = ""; // 保持默认
    private int unicode = 0; // 保持默认
    private int smooth = 0; // 保持默认
    private int aa = 0; // 保持默认
    private int stretchH = 100;
    // -------------------------------------- 🔼 info 🔼 --------------------------------------
    // -------------------------------------- 🔽 common 🔽 --------------------------------------
    // lineHeight=32 base=16 scaleW=128 scaleH=128 pages=1 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0
    private int lineHeight = 0; // 字体高度 size 属性 UI
    private int baseline = 0; // Baseline(字体基准线) UI
    private int scaleW = 0; // 图片资源宽度
    private int scaleH = 0; // 图片资源高度

    // 使用默认值
    private int pages = 1;
    private int packed = 0;
    private int alphaChnl = 1;
    private int redChnl = 0;
    private int greenChnl = 0;
    private int blueChnl = 0;
    // -------------------------------------- 🔼 common 🔼 --------------------------------------
    // -------------------------------------- 🔽 editor 🔽 --------------------------------------
    // editor id="FontMaker2" version="1.1.0" padding=0 extSpacing=0
    private String editorId = "";
    private String editorVersion = "";
    private int editorPadding = 0;
    private int editorExtSpacing = 0; // 字符间距(注意!!!空字符的xadvance跟着改变) UI
    // -------------------------------------- 🔼 editor 🔼 --------------------------------------
    // -------------------------------------- 🔽 page 🔽 --------------------------------------
    // id=0 file="-.png"
    private int pageId = 0;
    private String pageFile = ""; // 图集名字 UI
    // -------------------------------------- 🔼 page 🔼 --------------------------------------

    // 空格宽度 UI
    private int emptyWidth = 0;

    // -------------------------------------- 🔽 chars 🔽 --------------------------------------
    private List<BMChar> bmChars = new ArrayList<>();
    // -------------------------------------- 🔼 chars 🔼 --------------------------------------
    private String filePath = "";

    private Image image = null;

    public BMFont() {
    }


    public int getLineHeight() {
        return lineHeight;
    }

    public void setLineHeight(int charHeight) {
        this.lineHeight = charHeight;
    }

    public int getEmptyWidth() {
        return emptyWidth;
    }

    public void setEmptyWidth(int emptyWidth) {
        this.emptyWidth = emptyWidth;
    }

    public int getPadding() {
        return padding;
    }

    public void setPadding(int padding) {
        this.padding = padding;
    }

    public int getBaseline() {
        return baseline;
    }

    public void setBaseline(int baseline) {
        this.baseline = baseline;
    }

    public List<BMChar> getBmChars() {
        return bmChars;
    }

    public void setBmChars(List<BMChar> bmChars) {
        this.bmChars = bmChars;
    }

    public String getFace() {
        return face;
    }

    public void setFace(String face) {
        this.face = face;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getSpacing() {
        return spacing;
    }

    public void setSpacing(int spacing) {
        this.spacing = spacing;
    }

    public int getBold() {
        return bold;
    }

    public void setBold(int bold) {
        this.bold = bold;
    }

    public int getItalic() {
        return italic;
    }

    public void setItalic(int italic) {
        this.italic = italic;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public int getUnicode() {
        return unicode;
    }

    public void setUnicode(int unicode) {
        this.unicode = unicode;
    }

    public int getSmooth() {
        return smooth;
    }

    public void setSmooth(int smooth) {
        this.smooth = smooth;
    }

    public int getAa() {
        return aa;
    }

    public void setAa(int aa) {
        this.aa = aa;
    }

    public int getStretchH() {
        return stretchH;
    }

    public void setStretchH(int stretchH) {
        this.stretchH = stretchH;
    }

    public int getScaleW() {
        return scaleW;
    }

    public void setScaleW(int scaleW) {
        this.scaleW = scaleW;
    }

    public int getScaleH() {
        return scaleH;
    }

    public void setScaleH(int scaleH) {
        this.scaleH = scaleH;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getPacked() {
        return packed;
    }

    public void setPacked(int packed) {
        this.packed = packed;
    }

    public int getAlphaChnl() {
        return alphaChnl;
    }

    public void setAlphaChnl(int alphaChnl) {
        this.alphaChnl = alphaChnl;
    }

    public int getRedChnl() {
        return redChnl;
    }

    public void setRedChnl(int redChnl) {
        this.redChnl = redChnl;
    }

    public int getGreenChnl() {
        return greenChnl;
    }

    public void setGreenChnl(int greenChnl) {
        this.greenChnl = greenChnl;
    }

    public int getBlueChnl() {
        return blueChnl;
    }

    public void setBlueChnl(int blueChnl) {
        this.blueChnl = blueChnl;
    }

    public String getEditorId() {
        return editorId;
    }

    public void setEditorId(String editorId) {
        this.editorId = editorId;
    }

    public String getEditorVersion() {
        return editorVersion;
    }

    public void setEditorVersion(String editorVersion) {
        this.editorVersion = editorVersion;
    }

    public int getEditorPadding() {
        return editorPadding;
    }

    public void setEditorPadding(int editorPadding) {
        this.editorPadding = editorPadding;
    }

    public int getEditorExtSpacing() {
        return editorExtSpacing;
    }

    public void setEditorExtSpacing(int editorExtSpacing) {
        this.editorExtSpacing = editorExtSpacing;
    }

    public int getPageId() {
        return pageId;
    }

    public void setPageId(int pageId) {
        this.pageId = pageId;
    }

    public String getPageFile() {
        return pageFile;
    }

    public void setPageFile(String pageFile) {
        this.pageFile = pageFile;
    }

    public String[] toCharString() {
        String datas[] = new String[this.bmChars.size()];
        for (int i = 0; i < this.bmChars.size(); i++) {
            BMChar bmChar = this.bmChars.get(i);
            datas[i] = bmChar.getLetter();
        }
        return datas;
    }

    public void setFilePath(String path) {
        this.filePath = path;
        this.image = new ImageIcon(this.getPngPath()).getImage();
    }

    public String getFilePath() {
        return this.filePath;
    }

    public String getPngPath() {
        File file = new File(this.filePath);
        return file.getParent() + "/" + this.pageFile;
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public String getBMCharString() {
        String ret = "";
        for (BMChar bmChar : this.bmChars) {
            ret += bmChar.getLetter();
        }
        return ret;
    }

    /**
     * 得到空白字符串
     */
    public BMChar getEmptyBMChar() {
        int totalWidth = 0, count = 0;
        for (BMChar bmChar : this.bmChars) {
            totalWidth += bmChar.getWidth();
            count++;
            if (bmChar.getLetter().equals(" ")) {
                return bmChar;
            }
        }
        BMChar bmChar = new BMChar();
        bmChar.setLetter(" ");
        bmChar.setxAdvance(totalWidth / count);
        this.bmChars.add(bmChar);
        return bmChar;
    }

    /**
     * 得到空白字符串
     */
    public BMChar getFromBMChar() {
        int totalWidth = 0, count = 0;
        for (BMChar bmChar : this.bmChars) {
            totalWidth += bmChar.getWidth();
            count++;
            if (bmChar.getLetter().equals("\t")) {
                return bmChar;
            }
        }
        BMChar bmChar = new BMChar();
        bmChar.setLetter("\t");
        bmChar.setxAdvance((totalWidth / count) * 3);
        this.bmChars.add(bmChar);
        return bmChar;
    }

    public Rectangle getRect() {
        int x = 0, y = 0, dx = 0, dy = 0;
        for (BMChar bmChar : this.bmChars) {
            x = Math.min(x, bmChar.getX());
            y = Math.min(y, bmChar.getY());
            dx = Math.max(dx, bmChar.getX() + bmChar.getWidth() + padding);
            dy = Math.max(dy, bmChar.getY() + bmChar.getHeight() + padding);
        }
        Rectangle rect = new Rectangle(x, y, dx - x, dy - y);
        return rect;
    }
}
